package com.fak.wid;





import java.util.ArrayList;
import java.util.List;



import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;

import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.widget.AdapterView;
import android.view.View;

import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class favorit extends ListActivity  {

	public static DBAdapter db;
	public static String quote = "";
	private int[] rowID= new int[10000];
	@Override
	public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        
        db = new DBAdapter(this);
   	 db.open();        		       
   	 final List<String> PROFILES = new ArrayList<String>();
   	 Cursor c = db.getAllData();
   	int i=0;
        if (c.moveToFirst())
        {
            do {  
            	rowID[i]=c.getInt(0);     
           	 i++;
           	 PROFILES.add(c.getString(1));
          
          	  	} while (c.moveToNext());
        }
   	
       db.close();
        setListAdapter(new ArrayAdapter<String>(this,R.layout.favorit_xml, PROFILES));
        getListView().setTextFilterEnabled(true);
        
        final ListView lv = getListView();
  	  lv.setTextFilterEnabled(true);
  	
  	 
  	  lv.setOnItemLongClickListener(new OnItemLongClickListener()
  	  {

		public boolean onItemLongClick(AdapterView<?> parent, View view,
				int position, long id) {
			// TODO Auto-generated method stub
			profileDialog(rowID[(int) id]);
			quote=PROFILES.get(position).toString();
			return false;
		}
  		  
  		  
  		  
  	  });
  	  
  	  
  	  
  	  
  	  }
     
	
	@Override
public boolean onCreateOptionsMenu(Menu menu) {
		
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu_xml, menu);
    
    return true;
}
	@Override
public boolean onOptionsItemSelected(MenuItem item) {
   
    switch (item.getItemId()) {
    case R.id.quit:
        quit();
        return true;
  
    case R.id.Preferences:
    	
        startActivity(new Intent(this, Preferences.class));
        return true;
    case R.id.Help:
    	
        startActivity(new Intent(this, Help_activity.class));
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}
private void quit() {
	finish();
}


public void profileDialog(final long id)
{
	  final CharSequence[] items = {"Óäàëèòü","Îòïðàâèòü â"};

	  AlertDialog.Builder builder = new AlertDialog.Builder(this);
	  builder.setTitle("×òî ñäåëàòü");
	  builder.setItems(items, new DialogInterface.OnClickListener() {
	      public void onClick(DialogInterface dialog, int item) {
	    	  
	    	  if (item==0)
	    		{
	    		  finish();
	  	    	db.open();
	  			db.deleteData(id);
	  			db.close();
	  			startActivity(getIntent());
	        	} 
	    	  else 
	    	  {
	    		  
	    		  
	    		  
	    		  Intent sharingIntent = new Intent(Intent.ACTION_SEND);
	    	    	sharingIntent.setType("text/plain");
	    	    	sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, quote);
	    	    	sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Öèòàòà");
	    	    	sharingIntent.putExtra(android.content.Intent.EXTRA_TITLE, "Öèòàòà");
	    	    	startActivity(Intent.createChooser(sharingIntent, "Îïóáëèêîâàòü ÷åðåç:"));
	    		   		  
	      }
		
	      }
	  });
	  
	  AlertDialog alert = builder.create();
	  alert.show();
	
}

}

